package cn.xhz.gdal.model;

import javafx.util.Builder;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Feature;
import org.gdal.ogr.Layer;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * gdal的对象
 *
 * @author life-oss
 * @since 2022/12/13
 */
public class GDAL{
    /**
     * 数据源对象
     * 是需要关闭的
     */
    private DataSource dataSource;
    /**
     * 图层对象
     * 操作具体的图层
     */
    private Layer layer;
    /**
     * 数据集合
     */
    private Map<Feature, Map<String,String>> data=new LinkedHashMap<>();
    /**
     * 扩展属性
     */
    private Map<String,Object> extend=new LinkedHashMap<>();

    /**
     * 调用该方法可以在当前数据源删除该图层
     */
    public void removeLayer(){

    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Layer getLayer() {
        return layer;
    }

    public void setLayer(Layer layer) {
        this.layer = layer;
    }

    public GDAL() {
    }

    public static GDALBuilder builder(){
        return new GDALBuilder();
    }

    public Map<Feature, Map<String, String>> getData() {
        return data;
    }

    public void setData(Map<Feature, Map<String, String>> data) {
        this.data = data;
    }

    public Map<String, Object> getExtend() {
        return extend;
    }

    public void setExtend(Map<String, Object> extend) {
        this.extend.putAll(extend);;
    }
    public void setExtend(String filedName,Object object){
        this.extend.put(filedName,object);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GDAL gdal = (GDAL) o;
        return Objects.equals(dataSource, gdal.dataSource) && Objects.equals(layer, gdal.layer) && Objects.equals(data, gdal.data) && Objects.equals(extend, gdal.extend);
    }

    @Override
    public int hashCode() {
        return Objects.hash(dataSource, layer, data, extend);
    }

    /**
     * gdalbuilder
     *
     * @author life-oss
     * @since 2022/12/16
     */
    public static class GDALBuilder implements Builder<GDAL> {
        private GDAL gdal=new GDAL();
        public GDALBuilder dataSource(DataSource dataSource){
            gdal.setDataSource(dataSource);
            return this;
        }
        public GDALBuilder layer(Layer layer){
            gdal.setLayer(layer);
            return this;
        }
        public GDALBuilder data(Map<Feature,Map<String,String>> map){
            gdal.setData(map);
            return this;
        }
        @Override
        public GDAL build(){
            return this.gdal;
        }
    }
}
